﻿using System.Web.Mvc;
using System.Web.Routing;

namespace UIShell.OSGI.Mvc
{
    public static class RouteDataExtensions
    {
        public static string GetPluginSymbolicName(this RouteData routedata)
        {
            object symbolicName;
            if (routedata.DataTokens.TryGetValue("SymbolicName", out symbolicName))
            {
                return symbolicName as string;
            }
            if (routedata.DataTokens.TryGetValue("area", out symbolicName))
            {
                return symbolicName as string;
            }

            var castRoute = routedata.Route as Route;
            if (castRoute != null && castRoute.DataTokens != null)
            {
                return castRoute.DataTokens["area"] as string;
            }

            var routeWithArea = routedata.Route as IRouteWithArea;
            return routeWithArea != null ? routeWithArea.Area : null;
        }

        public static void SetPluginSymbolicName(this Route route, string symbolicName)
        {
            route.DataTokens["SymbolicName"] = symbolicName;
        }
    }
}
